#!/bin/bash
# export JAVA_HOME="" # Set here if not set in environment
# export CUDA_PATH="" # Set here if not set in environment

MEMSIZE="-Xmx14G"
export JAVA_OPTS="${MEMSIZE} -Xms128M -Dfile.encoding=UTF-8" # Set as much memory as possible
BIDMACH_ROOT="${BASH_SOURCE[0]}"
if [ ! `uname` = "Darwin" ]; then
  BIDMACH_ROOT=`readlink -f "${BIDMACH_ROOT}"`
else
  while [ -L "${BIDMACH_ROOT}" ]; do
    BIDMACH_ROOT=`readlink "${BIDMACH_ROOT}"`
  done
fi
BIDMACH_ROOT=`dirname "$BIDMACH_ROOT"`
pushd "${BIDMACH_ROOT}"  > /dev/null
BIDMACH_ROOT=`pwd -P`
BIDMACH_ROOT="$( echo ${BIDMACH_ROOT} | sed 's+/cygdrive/\(.\)+\1:+' )"
JCUDA_LIBDIR="${BIDMACH_ROOT}/lib"
LIBDIR="${BIDMACH_ROOT}/lib"

JCUDA_VERSION=`echo lib/jcuda-natives-*.jar | sed 's/.*jcuda-natives-//' | sed 's/-.*\.jar//'`
BIDMAT_VERSION=`echo lib/BIDMat-*-cpu-*.jar | sed 's/.*BIDMat-//' | sed 's/-cpu.*\.jar//'`

OSS=`uname -a`
if [ "$ARCH" = "" ]; then
    ARCH=`arch`
fi
if [[ "$OSS" == Darwin* ]] ; then
    if [[ "$OSS" == *x86_64 ]] ; then
	    ARCH="x86_64"
    fi
    OSS="apple"
elif [[ "$OSS" == Linux* ]] ; then
    OSS="linux"
elif [[ "$OSS" == CYGWIN* ]] ; then
    if [[ "$OSS" == *WOW* ]] ; then
        ARCH="x86_64"
    fi
	OSS="windows"
else
    echo "OS not supported" $OSS
    exit 1
fi

popd > /dev/null
if [ "$OSS" = "windows" ]; then
    if [ ! "${JAVA_HOME}" = "" ]; then
        JAVA_HOME=`${BIDMACH_ROOT}/shortpath.bat "${JAVA_HOME}"`
	    export JAVA_HOME=`echo ${JAVA_HOME} | sed 's_\\\\_/_g'`/bin
    fi
fi

BIDMACH_LIBS="${BIDMACH_ROOT}/target/BIDMach-${BIDMAT_VERSION}.jar;\
${LIBDIR}/BIDMach-${BIDMAT_VERSION}-cpu-${OSS}-${ARCH}.jar;\
${LIBDIR}/BIDMach-${BIDMAT_VERSION}-gpu-${OSS}-${ARCH}.jar;\
${LIBDIR}/BIDMat-${BIDMAT_VERSION}.jar;\
${LIBDIR}/BIDMat-${BIDMAT_VERSION}-cpu-${OSS}-${ARCH}.jar;\
${LIBDIR}/BIDMat-${BIDMAT_VERSION}-gpu-${OSS}-${ARCH}.jar;\
${LIBDIR}/IScala-1.0.0.jar;\
${LIBDIR}/ptplot-1.0.jar;${LIBDIR}/ptplotapplication-1.0.jar;${LIBDIR}/protobuf-java-3.1.0.jar;\
${LIBDIR}/jhdf5-3.2.1.jar;${LIBDIR}/jhdf5-3.2.1-${OSS}-${ARCH}.jar;\
${LIBDIR}/commons-math3-3.2.jar;${LIBDIR}/lz4-1.3.jar;${LIBDIR}/json-io-4.5.0.jar;\
${LIBDIR}/jfreechart-1.0.19.jar;${LIBDIR}/jcommon-1.0.23.jar"

JCUDA_LIBS=""
for lib in jcuda jcublas jcufft jcurand jcusolver jcusparse jcudnn; do
    JCUDA_LIBS="${JCUDA_LIBS};${LIBDIR}/${lib}-${JCUDA_VERSION}.jar;${LIBDIR}/${lib}-natives-${JCUDA_VERSION}-${OSS}-${ARCH}.jar"
done

ALL_LIBS=";${BIDMACH_LIBS};${JCUDA_LIBS};${JAVA_HOME}/lib/tools.jar"

if [ "$OSS" = "windows" ]; then
    if [ ! "${CUDA_PATH}" = "" ]; then
	    NEWPATH=`${BIDMACH_ROOT}/shortpath.bat "${CUDA_PATH}"`
	    NEWPATH=`echo $NEWPATH | sed 's_\\\\_/_g'`/bin
    fi
else
    ALL_LIBS=`echo "${ALL_LIBS}" | sed 's/;/:/g'`
fi

if [ "$1" = "notebook" ]; then
    shift 1
    if [ ! -z "$@" ]; then
        KERNEL_CMD="[\"java\", \"-cp\", \"${ALL_LIBS}\", \"${MEMSIZE}\", \"-Xms128M\", \"-Dfile.encoding=UTF-8\", \"org.refptr.iscala.IScala\", \"--profile\", \"{connection_file}\", \"--parent\", \"$@\"]"
    else
        KERNEL_CMD="[\"java\", \"-cp\", \"${ALL_LIBS}\", \"${MEMSIZE}\", \"-Xms128M\", \"-Dfile.encoding=UTF-8\", \"org.refptr.iscala.IScala\", \"--profile\", \"{connection_file}\", \"--parent\"]"
    fi
    ipython notebook --profile=scala --KernelManager.kernel_cmd="$KERNEL_CMD"
else
    ${BIDMACH_ROOT}/scripts/scala/scala -Dscala.repl.maxprintstring=8000 -nobootcp -toolcp "${ALL_LIBS}" -cp "${ALL_LIBS}" -Yrepl-sync -i ${LIBDIR}/bidmach_init.scala ${NOREPL} "$@"
fi



